// fpDoubleSingle.cpp: implementation of the fpDoubleSingle class.
//
// 17/7/2000 MF - change from john to fix font size problem on multi
//				  question numerics (width not increased when font size
//				  increased) - log ref: 26018.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "fpDoubleSingle.h"
#include "fpDoubleSinglePeer.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#ifdef FPCONTROLS
#ifndef _UNICODE

IMPLEMENT_DYNAMIC(fpDoubleSingle, AWTComponent)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

fpDoubleSingle::fpDoubleSingle()
{
	construct(CString(""), 0);
}

fpDoubleSingle::fpDoubleSingle(LPTSTR text)
{
	construct(CString(text), lstrlen(text));
}

fpDoubleSingle::fpDoubleSingle(CString& text)
{
	construct(text, text.GetLength());
}

fpDoubleSingle::fpDoubleSingle(int columns)
{
	construct(CString(""), columns);
}

fpDoubleSingle::~fpDoubleSingle()
{
	if (m_actionListener != NULL)
	{
		if (m_actionListener->isMultiCaster())
		{
			AWTEventMulticaster* mc = dynamic_cast<AWTEventMulticaster*>(m_actionListener);
			delete mc;
		}
	}
}

CWnd* fpDoubleSingle::createComponent()					 
{
	CWnd* wParent = getParent()->getPeer();
	if (wParent == NULL)
		return wParent;
	fpDoubleSinglePeer* pwnd = new fpDoubleSinglePeer(this);
	DWORD dwStyle = WS_CHILD | WS_VISIBLE;
	pwnd->Create(dwStyle, getBounds(), wParent, 0);
	pwnd->SetFont(CFont::FromHandle(m_font));
	pwnd->SetText(m_text.GetBuffer(0));
	pwnd->SetMarginTop(0);
	pwnd->SetMarginLeft(1);
	pwnd->SetAppearance(2);
	pwnd->SetButtonStyle(1);
	pwnd->SetAlignTextH(2);
	pwnd->SetFixedPoint(TRUE);
	if (m_decimalPlaces != -1)
		pwnd->SetDecimalPlaces(m_decimalPlaces);
	TCHAR s[128];
	sprintf(s, "%f", m_min);
	((fpDoubleSinglePeer*)pwnd)->SetMinValue(s);	
	sprintf(s, "%f", m_max);
	((fpDoubleSinglePeer*)pwnd)->SetMaxValue(s);	
	AWTColor* c = getForeground();
	if (c != NULL)
		pwnd->SetForeColor(c->getColorRef());
	if (!m_enabled)
		pwnd->EnableWindow(FALSE);
	return pwnd;
}

CSize fpDoubleSingle::preferredSize(int columns) 
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz(0,0);
	fpDoubleSinglePeer* pwnd = (fpDoubleSinglePeer*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	
	// 17/7/2000 mf 
	//TEXTMETRIC tm;
	//pdc->GetTextMetrics(&tm);
	//pdc->SelectObject(ftOld);
	//pwnd->ReleaseDC(pdc);	
	//sz.cx = (tm.tmAveCharWidth * columns) + 24;
	//sz.cy = tm.tmHeight + 6;

	TCHAR c[256];
	for (int n = 0; n < columns; n++)
		c[n] = 'X';
	c[columns] = '\0';
	GetTextExtentPoint32(pdc->m_hDC, c, columns, &sz);
	sz.cx += 6;
	sz.cy += 6;
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	// 17/7/2000 mf end

	return sz;
}

CSize fpDoubleSingle::preferredSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return preferredSize(m_columns);
}

CSize fpDoubleSingle::minimumSize(int columns) 
{
	if (m_peer == NULL)
		return CSize(0,0);
	CSize sz(0,0);
	fpDoubleSinglePeer* pwnd = (fpDoubleSinglePeer*)m_peer;
	CDC* pdc = pwnd->GetDC();
	CFont* ft = CFont::FromHandle(m_font);
	CFont* ftOld = pdc->SelectObject(ft);
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm);
	pdc->SelectObject(ftOld);
	pwnd->ReleaseDC(pdc);	
	sz.cx = (tm.tmAveCharWidth * columns) + 24;
	sz.cy = tm.tmHeight + 6;
	return sz;
}

CSize fpDoubleSingle::minimumSize() 
{
	if (m_peer == NULL)
		return CSize(0,0);
	return minimumSize(m_columns);
}

void fpDoubleSingle::setColumns(int columns)
{
	if (columns < 0) return;
	if (m_columns != columns)
	{
		m_columns = columns;
		invalidate();
	}
}

void fpDoubleSingle::setText(CString& text)
{
	m_text = text;
	if (m_peer != NULL)
	{	
//		m_peer->PostMessage(WM_AWTSETTEXT, 0, (LPARAM)new CString(text));
		((fpDoubleSinglePeer*)m_peer)->SetText(m_text.GetBuffer(0));	
	}
}

CString& fpDoubleSingle::getText()
{
	if (m_peer != NULL)
	{
		TCHAR sz[256];
		((fpDoubleSinglePeer*)m_peer)->GetText(sz, sizeof(sz));
		m_text = sz;
	}
	return m_text;
}

void fpDoubleSingle::setDecimalPlaces(int decimalPlaces)
{
	m_decimalPlaces = decimalPlaces;
	if (m_peer != NULL)
	{
		((fpDoubleSinglePeer*)m_peer)->SetDecimalPlaces(m_decimalPlaces);	
	}
}

void fpDoubleSingle::setMax(double max)
{
	m_max = max;
	if (m_peer != NULL)
	{
		TCHAR s[128];
		wsprintf(s, "%f", max);
		((fpDoubleSinglePeer*)m_peer)->SetMaxValue(s);	
	}
}

void fpDoubleSingle::setMin(double min)
{
	m_min = min;
	if (m_peer != NULL)
	{
		TCHAR s[128];
		wsprintf(s, "%f", min);
		((fpDoubleSinglePeer*)m_peer)->SetMinValue(s);	
	}
}

void fpDoubleSingle::setForeground(AWTColor* c)
{
	AWTComponent::setForeground(c);
	if (m_peer != NULL)
		((fpDoubleSinglePeer*)m_peer)->SetForeColor(c->getColorRef());
}

#endif
#endif //FPCONTROLD